package com.ldj.tim.server.config;

import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.ldj.tim.common.dto.P2PInfoDTO;
import com.ldj.tim.common.pojo.BasicMessage;
import com.ldj.tim.server.util.TimestampToWeekdayNumber;
import org.springframework.stereotype.Component;

/**
 * @author majinhui
 * @description
 * @create 2024/11/19 15:19
 **/
@Component
public class IdsTableNameHandler implements TableNameHandler {
    public static final ThreadLocal<P2PInfoDTO> REQUEST_DATA = new ThreadLocal<>();

    public static void initData(P2PInfoDTO message) {
        REQUEST_DATA.set(message);
    }

    @Override
    public String dynamicTableName(String sql, String tableName) {
        if(!tableName.contains("box")){
            return tableName;
        }
        P2PInfoDTO msg = REQUEST_DATA.get();
        int tableSuffix = TimestampToWeekdayNumber.getWeekdayNumber(msg.getTimestamp());
        String targetTableName = tableName + "_" + tableSuffix;
        REQUEST_DATA.remove();
        return targetTableName;
    }
}
